var mysql = require('mysql');
var superagent = require('superagent');

var msyqlConfig = require('./config').config.mysqlOptions;

let values = [];
let rootId = ''

function update() {
    values.length = 0;
    var connection1 = mysql.createConnection(msyqlConfig);
    let query1 = 'select * from topic where flag=0 limit 1';
    connection1.query(query1, function (err, res) {
        if (err) {
            console.log('query1 error');

        } else {
            console.log('query1 success');
            console.log();

            var list = res;
            if (list.length > 0) {
                var length = list.length;
                if (length > 0) {
                    rootId = list[0].id;
                    let title = list[0].name;
                    console.log(rootId);

                    console.log(title);
                    console.log();


                    let url = `https://www.zhihu.com/api/v3/topics/${rootId}/children`;
                    getData(url);
                }
            } else {
                console.log('no rest');

            }
        }
    });
    connection1.end();
}
update();

function getData(url) {
    console.log('start getData');
    console.log();

    superagent.get(url).end(function (err, res) {

        if (err) {
            console.log('err');
            console.log(err);

        } else {
            let o = JSON.parse(res.text);
            let paging = o.paging;
            let data = o.data;
            let isEnd = paging.is_end;
            let next = paging.next;
            if (data.length > 0) {
                data.forEach(v => {
                    var id = v.id;
                    var name = v.name;
                    let item = [id, name, 0];
                    values.push(item);
                });
                url = next;
                getData(url);
            } else {
                if (values.length > 0) {
                    console.log(values);

                    var connection2 = mysql.createConnection(msyqlConfig);
                    var query2 = 'insert ignore into topic values ?';
                    connection2.query(query2, [values], function (err, res) {
                        if (err) {
                            console.log('query2 error');

                        } else {
                            console.log('query2 success');
                            console.log();
                            console.log(res);
                            console.log();
                            updateFlag();
                        }
                    });
                    connection2.end();
                } else {
                    console.log('没有子话题');
                    updateFlag();
                }

            }
        }
    })
}

function updateFlag() {
    var connection3 = mysql.createConnection(msyqlConfig);
    let query3 = 'update topic set flag=1 where id=' + rootId;
    connection3.query(query3, function (err, res) {
        if (err) {
            console.log('query3 error');

        } else {
            console.log('query3 success');
            console.log();
            console.log(res);
            console.log();

            setTimeout(() => {

                update();
            }, 1000);
        }
    });
    connection3.end();
}